package com.unicom.projt;

import com.unicom.projt.http.AbstractHttpController;
import com.unicom.projt.http.UriConstants;
import com.unicom.projt.util.ToastUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 12-3-6
 * Time: 下午2:09
 */
public class UpgradeController extends AbstractHttpController<String> {
    private InputPhoneNumberPage inputPhoneNumberPage;

    public UpgradeController(InputPhoneNumberPage inputPhoneNumberPage) {
        super(String.class);
        this.inputPhoneNumberPage = inputPhoneNumberPage;
    }

    @Override
    public void onNetworkAbort() {
        ToastUtils.showMessage(inputPhoneNumberPage, "错误，网络连接被中断.");
        inputPhoneNumberPage.hideProgressDialog();
    }

    @Override
    public void onNetworkError() {
        ToastUtils.showMessage(inputPhoneNumberPage, "错误，网络连接错误.");
        inputPhoneNumberPage.hideProgressDialog();
    }

    @Override
    public void onRequestFinish(String response) {
        inputPhoneNumberPage.hideProgressDialog();
        if (response != null && response.split(",").length > 1) {
            String version = response.split(",")[0];
            String downloadUrl = response.split(",")[1];
            try {
                double updateVersion=Double.valueOf(version);
                if (Const.VERSION < updateVersion) {
                    inputPhoneNumberPage.setDownloadUrl(downloadUrl);
                    inputPhoneNumberPage.showUpgradeDialog();
                }
            } catch (NumberFormatException e) {
            }
        }
//        ToastUtils.showMessage(inputPhoneNumberPage,"当前版本是最新的");
    }

    @Override
    public void onResponseError() {
        ToastUtils.showMessage(inputPhoneNumberPage, "错误，服务器返回错误.");
        inputPhoneNumberPage.hideProgressDialog();
    }


    public void checkTheVersion() {
        this.sendRequest(UriConstants.UPGRADE, "");
    }
}
